package com.example.aiquestions.controller;

import com.example.aiquestions.entity.Paper;
import com.example.aiquestions.model.common.ApiResponse;
import com.example.aiquestions.service.PaperService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/paper")
public class PaperController {
    @Autowired
    private PaperService paperService;

    /**
     * 保存试卷
     */
    @PostMapping("/save")
    public ApiResponse<Paper> save(@RequestBody Paper paper) {
        return ApiResponse.success(paperService.save(paper));
    }

    /**
     * 更新试卷
     */
    @PostMapping("/update")
    public ApiResponse<Paper> update(@RequestBody Paper paper) {
        return ApiResponse.success(paperService.update(paper));
    }

    /**
     * 删除试卷
     */
    @GetMapping("/delete")
    public ApiResponse<Boolean> delete(@RequestParam("paperId") Long paperId) {
        return ApiResponse.success(paperService.delete(paperId));
    }

    /**
     * 获取试卷详情
     */
    @GetMapping("/getById")
    public ApiResponse<Paper> getById(@RequestParam("paperId") Long paperId) {
        return ApiResponse.success(paperService.getById(paperId));
    }

    /**
     * 分页获取试卷列表
     */
    @GetMapping("/getList")
    public ApiResponse<PageInfo<Paper>> getList(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "subjectId", required = false) Long subjectId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return ApiResponse.success(paperService.getList(userId, subjectId, pageNum, pageSize));
    }
}
